<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 *
 * @package  module
 * @subpackage  base/shop
 * @author   allen.qiang
 * @version 1.0
 */

class shopCheckout {


    /**
     * 查询会员全部发票信息
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买记录
     * @return array|null
     */
    public function getInvoiceList( &$bill_master_array, &$cust_buy_log ) {

        //加载会员发票类
        quanli_require('module/base/customer/custInvoice.class.php');

        //实例会员发票类
        $custInvoice = new custInvoice();

        //查询发票信息
        $arg_array = array(
            'pcustID' => $cust_buy_log['pcustID']
        );
        $invoice_array = $custInvoice->searchCustInvoice( $arg_array );

        if ($invoice_array) {
            foreach($invoice_array as &$invoice) {
                if ($invoice['invoiceID'] == $cust_buy_log['invoiceID']) {
                    $bill_master_array['invoice'] = 1;
                    $bill_master_array['invoiceID'] = $invoice['invoiceID'];
                    $bill_master_array['invoice_type'] = $invoice['invoice_type'];
                    $bill_master_array['invoice_title_type'] = $invoice['invoice_title_type'];
                    $bill_master_array['invoice_title'] = $invoice['invoice_title'];
                    $cust_buy_log['invoiceID'] = $bill_master_array['invoiceID'];
                }
            }
        }

        $bill_master_array['invoice'] = 0;
        $bill_master_array['invoiceID'] = 0;
        $bill_master_array['invoice_type'] = null;
        $bill_master_array['invoice_title_type'] = null;
        $bill_master_array['invoice_title'] = null;

        return $invoice_array;

    }

    /**
     * 查询会员收货地址列表
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @param object $customerHandler 会员类实例
     * @return null|array
     */
    public function searchDeliverList( &$bill_master_array, &$cust_buy_log ) {

        //加载会员地址类
        quanli_require('module/base/customer/custDeliver.class.php');

        //实例会员地址类
        $custDeliver = new custDeliver();

        $arg_array = array(
            'pcustID' => $cust_buy_log['pcustID'],
            'sortname' => 'deliverID',
            'sortorder' => 'DESC'
        );

        //查询会员收货地址
        $deliver_array = $custDeliver->searchCustDeliver($arg_array);
        if (!$deliver_array)
            return null;

        $deliverFlg = 0;
        foreach($deliver_array as $deliver) {
            if ($deliverFlg == 0 || $deliver['deliverID'] == $cust_buy_log['deliverID']) {
                ++$deliverFlg;
                $bill_master_array['deliverid'] = $deliver['deliverID'];
                $bill_master_array['deliver_province'] = $deliver['provinceName'];
                $bill_master_array['deliver_city'] = $deliver['cityName'];
                $bill_master_array['deliver_county'] = $deliver['countyName'];
                $bill_master_array['deliver_address'] = $deliver['address'];
                $bill_master_array['deliver_name'] = $deliver['custName'];
                $bill_master_array['deliver_postalCode'] = $deliver['postalCode'];
                $bill_master_array['deliver_mobile'] = $deliver['mobile'];
                $bill_master_array['deliver_phone'] = $deliver['phone'];
                $bill_master_array['countyid'] = $deliver['countyid'];
                $bill_master_array['cityid'] = $deliver['cityid'];
                $bill_master_array['provinceid'] = $deliver['provinceid'];
                $bill_master_array['longitude'] = $deliver['longitude'];
                $bill_master_array['latitude'] = $deliver['latitude'];
                $bill_master_array['precise'] = $deliver['precise'];
            }
        }

        $cust_buy_log['deliverID'] = $bill_master_array['deliverid'];

        return $deliver_array;

    }

    /**
     * 根据收货地址查询配送站和配送员
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @return array|null
     */
    public function getDistributionList( &$bill_master_array ) {

        quanli_require('module/base/system/mapAreaZoning.class.php');
        quanli_require('module/base/system/sysDistribution.class.php');
        quanli_require('module/base/system/sysDistributionTechnician.class.php');

        //实例化配送方式类
        $mapAreaZoning = new mapAreaZoning( );

        //实例化配送站设置类
        $sysDistribution = new sysDistribution( );

        //实例化配送员类
        $sysDistributionTechnician = new sysDistributionTechnician( );

        $bill_master_array['distribution_id'] = 0;
        $bill_master_array['distribution_name'] = null;
        $bill_master_array['distribution_technician_id'] = 0;
        $bill_master_array['distribution_technician_name'] = null;

        //查询城市下的所有地图区域
        $arg_array = array(
            'cityID' => $bill_master_array['cityid'],
            'type' => 1,
            'brandID' => G_BRANDID_DEFAULT
        );

        $map_area_array = $mapAreaZoning->searchMapArea( $arg_array );

        if(!$map_area_array){
            return null;
        }

        //城市经纬度
        $lgtLat = $bill_master_array['longitude'].','. $bill_master_array['latitude'];

        $areaID = 0;
        foreach($map_area_array as $area){

            //经纬度集合
            $lgtLat_array = array();
            $map_area_detail_array = $mapAreaZoning->searchMapAreaPath( $area['areaID'] );
            foreach($map_area_detail_array as $area_detail){
                $lgtLat_array[] = $area_detail['longitude'].','.$area_detail['latitude'];
            }

            //通函数筛选出匹配的经纬度区域  匹配到返回区域ID  否则返回false
            $filter_result = $this->area_filter($lgtLat,$lgtLat_array);
            if(!$filter_result){
                continue;
            }

            if($areaID == 0){
                $areaID = $area['areaID'];
                break;
            }

        }

        if($areaID == 0)
            return null;

        //查询地图区域和配送站的映射关系
        $arg_array = array(
            'areaID' => $areaID
        );
        $distribution_map_area = $sysDistribution->searchDistributionMapArea( $arg_array );
        if(!$distribution_map_area)
            return null;

        //查询配送站
        $arg_array = array(
            'distribution_id' => $distribution_map_area[0]['distribution_id'],
            'useFlg' => 1
        );
        $distribution_array = $sysDistribution->search( $arg_array );
        if(!$distribution_array)
            return null;

        $bill_master_array['distribution_id'] = $distribution_array[0]['distribution_id'];
        $bill_master_array['distribution_name'] = $distribution_array[0]['distribution_name'];

        //查询配送员
        $arg_array = array(
            'distribution_id' => $distribution_array[0]['distribution_id'],
            'useFlg' => 1
        );
        $distributionTechnician_array = $sysDistributionTechnician->search( $arg_array );

        if(!$distributionTechnician_array)
            return null;

        $bill_master_array['distribution_technician_id'] = $distributionTechnician_array[0]['distribution_technician_id'];
        $bill_master_array['distribution_technician_name'] = $distributionTechnician_array[0]['distribution_technician_name'];

    }

    /**
     * 根据收货地址查询配送方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @return array|null
     */
    public function searchSendTypeList( &$bill_master_array, &$cust_buy_log = null ) {

        quanli_require('module/base/system/sysSend.class.php');
        quanli_require('module/base/system/mapAreaZoning.class.php');

        //实例化配送方式类
        $sysSend = new sysSend( );

        //实例化配送方式类
        $mapAreaZoning = new mapAreaZoning( );

        //查询城市下的所有地图区域
        $arg_array = array(
            'cityID' => $bill_master_array['cityid'],
            'type' => 2,
            'brandID' => G_BRANDID_DEFAULT
        );

        $map_area_array = $mapAreaZoning->searchMapArea( $arg_array );

        if(!$map_area_array){
            return null;
        }

        //城市经纬度
        $lgtLat = $bill_master_array['longitude'].','.$bill_master_array['latitude'];

        $areaID = 0;
        foreach($map_area_array as $area){

            //经纬度集合
            $lgtLat_array = array();
            $map_area_detail_array = $mapAreaZoning->searchMapAreaPath( $area['areaID'] );
            foreach($map_area_detail_array as $area_detail){
                $lgtLat_array[] =  $area_detail['longitude'].','.$area_detail['latitude'];
            }

            //通函数筛选出匹配的经纬度区域  匹配到返回区域ID  否则返回false
            $filter_result = $this->area_filter($lgtLat,$lgtLat_array);
            if(!$filter_result){
                continue;
            }

            if($areaID == 0){
                $areaID = $area['areaID'];
                break;
            }
        }

        if($areaID == 0){
            return null;
        }

        //查询地图区域下的所有配送区域
        $arg_array = array(
            'areaID' => $areaID
        );

        $send_area_array = $sysSend->searchSendArea( $arg_array );

        if(!$send_area_array){
            return null;
        }

        $return_array = array();

        $sendFlg = 0;
        foreach($send_area_array as &$send){

            //根据配送区域查询配送方式
            $arg_array = array(
                'sendTypeID' => $send['sendTypeID']
            );

            $sendType_array = $sysSend->searchSendType( $arg_array );

            if(!$sendType_array)
                continue;

            $sendType_array = $sendType_array[0];
            $return_array[$send['sendTypeID']] = array(
                'sendTypeID' => $sendType_array['sendTypeID'],
                'sendTypeName' => $sendType_array['sendTypeName'],
                'send_region_id' => $send['send_region_id'],
                'send_region_name' => $send['send_region_name'],
                'description' => $sendType_array['description'],
            );

            if ($sendFlg == 0 || (!is_null($cust_buy_log) && $send['send_region_id'] == $cust_buy_log['send_region_id'])) {
                ++$sendFlg;
                $bill_master_array['sendTypeID'] = $sendType_array['sendTypeID'];
                $bill_master_array['sendTypeName'] = $sendType_array['sendTypeName'];
                $bill_master_array['send_region_id'] = $send['send_region_id'];
                $bill_master_array['send_region_name'] = $send['send_region_name'];
                $bill_master_array['extend_1'] = null;
                $bill_master_array['extend_9'] = null;
                $bill_master_array['extend_10'] = null;
                $bill_master_array['extend_2'] = null;
                $bill_master_array['extend_11'] = null;
            }

        }

        if(empty($return_array)){
            return null;
        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['send_region_id'] = $bill_master_array['send_region_id'];
        }

        return $return_array;

    }

    /**
     * 根据配送方式，查询支付方式
     * @param string $source 来源
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @return array|null
     */
    public function getPayTypeList( $source, &$bill_master_array, &$cust_buy_log=null ) {

        //加载支付方式类
        quanli_require('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment();

        //查询支付类型
        $arg_array = array(
            'sendTypeID' => $bill_master_array['sendTypeID'],
            'sortname' => 'pay_type',
            'sortorder' => 'ASC'
        );
        $pay_array = $sysPayment->searchPayTypeFromSendType( $arg_array );
        if (!$pay_array)
            return null;

        $item_array = array();

        foreach($pay_array as &$item) {

            //根据支付类型查询支付方式
            $arg_array = array (
                'source' => $source,
                'type' => $item['pay_type'],
                'sortname' => 'payTypeID',
                'sortorder' => 'ASC',
                'useFlg' => 1
            );

            $pay_type_array = $sysPayment->searchPayType( $arg_array );
            if (!$pay_type_array)
                continue;

            foreach($pay_type_array as &$pay_type) {

                $payFlg = 0;
                if ($payFlg == 0 || (!is_null($cust_buy_log) && $pay_type['payTypeID'] == $cust_buy_log['payTypeID'])) {
                    ++$payFlg;
                    $bill_master_array['pay_type'] = $pay_type['type'];
                    $bill_master_array['payTypeID'] = $pay_type['payTypeID'];
                    $bill_master_array['payTypeName'] = $pay_type['payTypeName'];
                }
            }

            $item_array[] = array(
                'sendTypeID' => $item['sendTypeID'],
                'type' => $item['pay_type'],
                'list' => $pay_type_array
            );
        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['pay_type'] = null;
            $cust_buy_log['payTypeID'] = null;
        }

        return $item_array;

    }

    /**
     * 根据支付方式，查询付款方式
     * @param string $source 来源
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @return array|null
     */
    public function     getPaymentMethodList( &$bill_master_array, &$cust_buy_log=null ) {

        //加载支付方式类
        quanli_require('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment( $this->dbca );

        if(!isset($bill_master_array['payTypeID']) || empty($bill_master_array['payTypeID'])){
            return null;
        }

        //根据支付类型查询支付方式
        $arg_array = array(
            'payTypeID' => $bill_master_array['payTypeID'],
            'type' => 1     //货到付款类型
        );
        $pay_type_array = $sysPayment->searchPayType( $arg_array );

        if (!$pay_type_array){

            $bill_master_array['payment_method_id'] = 0;
            $bill_master_array['payment_method_name'] = null;
            $bill_master_array['payment_method_type'] = null;
            return null;
        }

        foreach($pay_type_array as &$pay_type) {

            $payment_method_array =  $sysPayment->searchPaymentMethod($arg_array);

            if(!$payment_method_array){
                continue;
            }

            foreach($payment_method_array as $payment){
                $paymentFlg = 0;
                if ($paymentFlg == 0 || (!is_null($cust_buy_log) && $pay_type['payTypeID'] == $cust_buy_log['payTypeID'])) {
                    ++$paymentFlg;
                    $bill_master_array['payment_method_id'] = $payment['id'];
                    $bill_master_array['payment_method_name'] = $payment['name'];
                    $bill_master_array['payment_method_type'] = $payment['type'];
                }
            }

            $item_array[] = array(
                'payTypeID' => $pay_type['payTypeID'],
                'type' => $pay_type['type'],
                'list' => $payment_method_array
            );

        }

        if(empty($item_array)){
            return null;
        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['payment_method_id'] = $bill_master_array['payment_method_id'];
        }

        return $item_array;

    }


    /**
     * 根据配送区域和商品总重量，计算运费
     * @param $send_area_id
     * @param $goods_weight
     * @return float|int|null
     */
    public function calculateFreight($send_area_id, $goods_weight) {

        $send_area_obj = new entity('sys_send_area');
        $send_area_obj->send_region_id = $send_area_id;
        if (!$this->dbca->fetch($send_area_obj))
            return -1;

        if ($send_area_obj->ykg >= $goods_weight)
            return $send_area_obj->freight;
        else {
            $overweight = ceil($goods_weight - $send_area_obj->ykg);
            return round($send_area_obj->freight + $overweight * $send_area_obj->overweight_freight, 2);
        }

    }

    /**
     * 查询配送网点列表
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @return array|null
     */
    public function checkoutBranchList( &$bill_master_array, &$cust_buy_log ) {
        return null;

        //查询自提点
        $arg_array = array(
            'provinceid' => $bill_master_array['provinceid'],
            'cityid' => $bill_master_array['cityid'],
            'countyid' => $bill_master_array['countyid'],
            'useFlg' => 1
        );
        $branch_array = $this->searchBranchInfo( $arg_array );
        if (!$branch_array) {
            return null;
        }

        if (isset($bill_master_array['extend_1'])) {

            //循环自提点数组
            foreach($branch_array as &$branch) {
                if ($branch['branch_id'] == $cust_buy_log['extend_1']) {
                    $bill_master_array['extend_1'] = $branch['branch_id'];
                    $bill_master_array['extend_9'] = $branch['branch_name'];
                    $bill_master_array['extend_10'] = $branch['branch_code'];
                }
            }
            $cust_buy_log['extend_1'] = $bill_master_array['extend_1'];


        }

        return $branch_array;

    }

    /**
     * 订单结算
     * 根据配送方式，查询支付方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @return array|null
     */
    public function searchPaymentList( &$bill_master_array, &$cust_buy_log=null ) {

        //加载支付方式类
        quanli_include('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment( $this->dbca );

        //查询支付类型
        $arg_array = array(
            'sendTypeID' => $bill_master_array['sendTypeID'],
            'sortname' => 'pay_type',
            'sortorder' => 'ASC'
        );
        $pay_array = $sysPayment->searchPayTypeFromSendPay( $arg_array );
        if (!$pay_array)
            return null;

        foreach($pay_array as &$item) {

            //根据支付类型查询支付方式
            $arg_array = array(
                'type' => $item['pay_type'],
                'sortname' => 'payTypeID',
                'sortorder' => 'ASC',
                'useFlg' => 1
            );

            $pay_type_array = $sysPayment->searchPayType( $arg_array );
            if (!$pay_type_array)
                continue;

            $item_array[] = array(
                'sendTypeID' => $item['sendTypeID'],
                'type' => $item['pay_type'],
                'list' => $pay_type_array
            );


            foreach($pay_type_array as &$pay_type) {
                if (!isset($bill_master_array['payTypeID']) || (!is_null($cust_buy_log) && $pay_type['payTypeID'] == $cust_buy_log['payTypeID'])) {
                    $bill_master_array['pay_type'] = $pay_type['type'];
                    $bill_master_array['payTypeID'] = $pay_type['payTypeID'];
                    $bill_master_array['payTypeName'] = $pay_type['payTypeName'];
                }
            }

        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['pay_type'] = $bill_master_array['pay_type'];
            $cust_buy_log['payTypeID'] = $bill_master_array['payTypeID'];
        }

        return $item_array;

    }

}